#!/bin/bash
#
# install service 安装脚本 2016-09-01
#

cd $(cd `dirname $0`; pwd)
echo "begin to install services"
sh printLine.sh '='
echo "pwd: " `pwd`
# test nas
test ! -d "/var/nas" && echo "Error! nas dir not found." && exit -1

mountStatus=`mount | grep "/var/nas"`
test -z "$mountStatus" && echo "Error! nfs dir mount error." && exit -1


# test master ip
masterIp=`ip a | grep inet | grep -v inet6 | grep -v 127.0.0.1|grep brd| awk '{print $2}'|awk -F '/' '{print $1}'`
echo "master IP is : $masterIp"
test -z "$masterIp" && \
    echo "install-dns.sh masterIp get error!" \
    && exit -1;
#get registry ip
#sh getDockerRegistry.sh
#dockerRegistry=`cat /usr/local/etc/registry`
#if [[ -z "$dockerRegistry" ]]; then
#	echo "Error! dockerRegistry set error."
#	sh printLine.sh 'x'
#	exit -1;
#fi
sh getNetIp.sh
netIp=`cat /usr/local/etc/netIp`
if [[ -z "$netIp" ]]; then
	echo "Error! netIp set error."
	sh printLine.sh 'x'
	exit -1;
fi
if [ ! -d "../data" ] || [ ! -d "../model" ] || [ ! -d "../nginx" ]; then
	echo "Error! required file not found."
	sh printLine.sh 'x'
	exit -1
fi
test ! -d "/var/nas/data" && cp -r ../data /var/nas/
test ! -d "/var/nas/model" && cp -r ../model /var/nas/
test ! -d "/var/nas/nginx" && cp -r ../nginx /var/nas/
test ! -d "/var/nas/yaml" && cp -r ../yaml /var/nas/
sed -i "s/{bec-host}/$masterIp/g" /var/nas/nginx/module/*
test ! -d "/var/nas/apps" && mkdir -p /var/nas/apps
test ! -d "/var/nas/extends" && mkdir -p /var/nas/extends

for item in apps data dns nas nginx
do
	check='yes'
	echo "begin to install $item .."
	sh printLine.sh '=' 2
	# pv pvc
	for category in pv pvc 
	do
		if [[ ! -f "/var/nas/yaml/$item/$category-$item.yaml" ]]; then
		 	echo "Error ! /var/nas/yaml/$item/$category-$item.yaml not found." 
		 	check='no'
		fi   
	done
	if [[ $check == 'no' ]]; then
		echo "$item file check status : no. continue"
		continue;
	fi

	for category in pv pvc 
	do
		exists=`kubectl get $category|grep $item`
		if [[ -z "$exists" ]]; then
			kubectl create -f "/var/nas/yaml/$item/$category-$item.yaml"
		else
			echo "$category $item already exists, jump to next."
			continue;
		fi
	done

	# rc svc
	if [[ "$item" == 'dns' ]] || [[ "$item" == 'nginx' ]]; then
		for category in rc svc 
		do
			if [[ ! -f "/var/nas/yaml/$item/$category-$item.yaml" ]]; then
			 	echo "Error ! /var/nas/yaml/$item/$category-$item.yaml not found." 
			 	check='no'
			fi   
		done
		if [[ $check == 'no' ]]; then
			echo "$item  file check status : no. continue"
			continue;
		fi
		#sed -i "s/{dockerRegistry}/$dockerRegistry/g" ../yaml/$item/rc-$item.yaml 
		sed -i "s/{masterIp}/$masterIp/g" /var/nas/yaml/$item/rc-$item.yaml 
		sed -i "s/{netIp}/$netIp/g" /var/nas/yaml/$item/svc-$item.yaml 
		
		for category in svc rc 
		do
			exists=`kubectl get $category|grep $item`
			if [[ -z "$exists" ]]; then
				kubectl create -f "/var/nas/yaml/$item/$category-$item.yaml"
			else
				echo "$category $item already exists, jump to next."
				continue;
			fi
		done
	fi
done
echo "install services end"
sh printLine.sh '='


